package Threads.CAS;



public class Account_impl implements Account{
    private Integer balance;

    public Account_impl(Integer balance){
        this.balance=balance;
    }

    @Override
    public Integer getBalance() {
        return this.balance;
    }

    @Override
    public void withdraw(Integer amount) {
        test2(amount);
    }

    //第一种实现 可能会造成多线程问题，结果不对
    public void test1(Integer amount){
        balance-= amount;//这一步可能会造成指令交错，导致结果不一致，可以选择加锁
    }

    //第二种实现，加syn锁
    public synchronized void test2(Integer amount){
        balance-= amount;
    }




    public static void main(String[] args) {
        Account.demo(new Account_impl(10000));
    }
}
